package com.nengjiang.wsplayer.activity;

import android.Manifest;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.view.View;

import com.nengjiang.wsplayer.EventListener;
import com.nengjiang.wsplayer.R;
import com.nengjiang.wsplayer.base.BaseThemeActivity;
import com.nengjiang.wsplayer.bean.CarmaResponse;
import com.nengjiang.wsplayer.bean.VideoResponse;
import com.nengjiang.wsplayer.oksocket.OkManager;
import com.nengjiang.wsplayer.utils.BackHandlerHelper;
import com.nengjiang.wsplayer.utils.SimplePlayer;
import com.tbruyelle.rxpermissions2.RxPermissions;

public class WaitActivity extends BaseThemeActivity {
    public static final String TAG = "WaitActivity";
    private long lastBackPress;

    private String pushCarmaUrl = null;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_wait);
        OkManager.getInstance().addEventListener(new EventListener() {
            @Override
            public void onVideoRes(VideoResponse videoResponse) {
                if (videoResponse != null) {
                    goVideo(videoResponse.data.videoStreamUrl);
                }
            }

            @Override
            public void onCarmaRes(CarmaResponse carmaResponse) {
                if (carmaResponse != null) {
                    showToast(carmaResponse.data.cameraStreamUrl);
                    pushCarmaUrl = carmaResponse.data.cameraStreamUrl;
                }
            }
        });
    }

    public void goVideo(String url) {
        Intent intent = new Intent(this, SimplePlayer.class);
        intent.putExtra("url", url);
        startActivity(intent);
    }

    @Override
    public void onBackPressed() {
        if (!BackHandlerHelper.handleBackPress(this)) {
            if (System.currentTimeMillis() - lastBackPress < 1000) {
                finish();
                super.onBackPressed();
            } else {
                lastBackPress = System.currentTimeMillis();
                showToast("再按一次退出");
            }
        }
    }

    public void startPush(View view) {

        if (Build.VERSION.SDK_INT >= 23) {
            RxPermissions rxPermissions = new RxPermissions(this);
            rxPermissions.request(
                    Manifest.permission.READ_EXTERNAL_STORAGE,
                    Manifest.permission.WRITE_EXTERNAL_STORAGE,
                    Manifest.permission.CAMERA,
                    Manifest.permission.RECORD_AUDIO
            ).subscribe(aBoolean -> {
                if (aBoolean) {
                    startRtspActivity();
                } else {
                    showToast("请相机及SD卡读写权限必要权限");
                }


            });
        } else {
            startRtspActivity();
        }

    }

    public void startPushCarma(String pushCarmaUrl) {
        if (Build.VERSION.SDK_INT >= 23) {
            RxPermissions rxPermissions = new RxPermissions(this);
            rxPermissions.request(
                    Manifest.permission.READ_EXTERNAL_STORAGE,
                    Manifest.permission.WRITE_EXTERNAL_STORAGE,
                    Manifest.permission.CAMERA,
                    Manifest.permission.RECORD_AUDIO
            ).subscribe(aBoolean -> {
                if (aBoolean) {
                    startRtspActivity();
                } else {
                    showToast("请相机及SD卡读写权限必要权限");
                }


            });
        } else {
            startRtspActivity();
        }

    }

    private void startRtspActivity() {
//        if(pushCarmaUrl == null){
//            showToast("没有获取到推流地址！");
//            return;
//        }
        Intent intent = new Intent(this, ExampleRtspActivity.class);
        intent.putExtra("pushCarmaUrl",pushCarmaUrl);
        startActivity(intent);
    }


}